// JavaScript Document
/*
Title: RSVP Reader (Firefox Extension) v 0.0.9
Creator: Robert Fentress (rfentres@vt.edu) with improvement by Lukas Vrbecky
Date: 6/20/2008
Description: This script allows users to read selected text on pages using Rapid Serial Visual Presentation.
License: This work is licensed under a Creative Commons License (http://creativecommons.org/licenses/by-sa/3.0/)
*/

window.addEventListener("load", jsreaderInit, true);
var jsreaderN = 0;
var jsreaderDelay = 300;
var jsreaderWordArray = new Array();
var jsreaderStopped = true;
var jsreaderPaused = true;
var jsreaderMouseOut = true;
var	jsreaderRewindStopped = true;
var jsreaderAlreadyPaused = false;
var jsreaderPaceNode;
var jsreaderCenterNode;
var jsreaderLeftNode;
var jsreaderRewindNode;
var jsreaderStopNode;
var jsreaderPauseNode;
var jsreaderPlayNode;
var jsreaderSmallerNode;
var jsreaderBiggerNode;
var jsreaderTextField;
var jsreaderReadSize = 1;
var lastTxt = "";
var jsreaderMaxLength = 10;
var jsreaderLess;
var jsreaderMore;
var playImg = "chrome://jsreader/skin/jsreader_play.gif";
var playDisImg = "chrome://jsreader/skin/jsreader_play_disabled.gif";
var stopImg = "chrome://jsreader/skin/jsreader_stop.gif";
var stopDisImg = "chrome://jsreader/skin/jsreader_stop_disabled.gif";
var pauseImg = "chrome://jsreader/skin/jsreader_pause.gif";
var pauseDisImg = "chrome://jsreader/skin/jsreader_pause_disabled.gif";
var rewindImg = "chrome://jsreader/skin/jsreader_rewind.gif";
var rewindDisImg = "chrome://jsreader/skin/jsreader_rewind_disabled.gif";
var leftImg = "chrome://jsreader/skin/jsreader_left.gif";
var leftDisImg = "chrome://jsreader/skin/jsreader_left_disabled.gif";
var centerImg = "chrome://jsreader/skin/jsreader_center.gif";
var centerDisImg = "chrome://jsreader/skin/jsreader_center_disabled.gif";


function jsreaderInit() {
	jsreaderPaceNode = document.getElementById("jsreader-pace");
	jsreaderMaxLengthNode = document.getElementById("jsreader-length");
	jsreaderLess = document.getElementById("jsreader-less");
	jsreaderMore = document.getElementById("jsreader-more");
	jsreaderCenterNode = document.getElementById("jsreader-center");
	jsreaderLeftNode = document.getElementById("jsreader-left");
	jsreaderRewindNode = document.getElementById("jsreader-rewind");
	jsreaderStopNode = document.getElementById("jsreader-stop");
	jsreaderPauseNode = document.getElementById("jsreader-pause");
	jsreaderPlayNode = document.getElementById("jsreader-play");
	jsreaderSmallerNode = document.getElementById("jsreader-smalller");
	jsreaderBiggerNode = document.getElementById("jsreader-bigger");
	jsreaderTextField = document.getElementById("jsreader-textfield");
	jsreaderPaceNode.value = jsreaderDelay;
	jsreaderMaxLengthNode.value = jsreaderMaxLength;
	jsreaderStop();
	jsreaderCenter();
}
function jsreaderReadSel()
{
	if (jsreaderStopped || jsreaderPaused) {
		document.getElementById("jsreader-context-link-menu").setAttribute("collapsed", true);
		var txt = content.document.getSelection();
		var temptxt;
		lastText = txt;
		if (txt != '') {
			jsreaderPauseNode.disabled = false;
			jsreaderPlayNode.disabled = true;
			jsreaderRewindNode.disabled = false;
			jsreaderStopNode.disabled = false;
			jsreaderMaxLengthNode.disabled = true;
			
			jsreaderLess.disabled = true;
			jsreaderMore.disabled = true;
	
			jsreaderPauseNode.image = pauseImg;
			jsreaderPlayNode.image = playDisImg;
			jsreaderRewindNode.image = rewindImg;
			jsreaderStopNode.image = stopImg;
			
			txt = txt.replace(/'/g, "\\'");
			txt = txt.replace(/\r/g, " ");
			txt = txt.replace(/\n/g, " &#182; ");
			jsreaderWordArray = new Array();
			jsreaderWordArrayFirst = new Array();
			jsreaderWordArrayFirst = txt.split(" ");
			for (var i=0; i<jsreaderWordArrayFirst.length; i++) {
        temptxt = jsreaderWordArrayFirst[i];
        while ( ( (i+1) < jsreaderWordArrayFirst.length ) && 
                ( makeLonger(jsreaderWordArrayFirst[i]) == "0" ) &&
                (temptxt.length + jsreaderWordArrayFirst[i+1].length <= jsreaderMaxLength )
              ) {
          temptxt = temptxt + " " + jsreaderWordArrayFirst[++i];
        }        
        jsreaderWordArray.push(temptxt);
			} 
			setTimeout("jsreaderSetText('"+jsreaderWordArray[jsreaderN]+"')", Math.round((1000*60)/jsreaderDelay));
			jsreaderStopped = false;
			jsreaderPaused = false;
		} else {
			alert("no text selected");
		}
	}
}
function jsreaderSetText(str) {
	if (lastText != content.document.getSelection()) {
		jsreaderStop();
	} else {
		var padding = 0;
		if (jsreaderWordArray[jsreaderN]!="&#182;") {
			if (!jsreaderStopped) jsreaderTextField.value = str;
		}
		if ((jsreaderN+1 < jsreaderWordArray.length) && (jsreaderStopped==false)) {
			// If there is any mid-sentence punctuation add some time padding
			if ( makeLonger(jsreaderWordArray[jsreaderN]) == "longer" ) { padding = Math.round((1000*60)/jsreaderDelay); }
			// If there is any end-of-sentence punctuation add some longer time padding
			if ( makeLonger(jsreaderWordArray[jsreaderN]) == "longest" ) { padding = Math.round((1000*60)/jsreaderDelay)*1.5; }
			
      jsreaderNewDelay = Math.round((1000*60)/jsreaderDelay) + padding;
			setTimeout("jsreaderSetText('"+jsreaderWordArray[++jsreaderN]+"')", jsreaderNewDelay);
		} else {
			jsreaderStopped = true;
			jsreaderPaused = false;
		}
	}
}

function makeLonger(textElement){
  if (
		(textElement.lastIndexOf(",") == textElement.length-1) || 
		(textElement.lastIndexOf(";") == textElement.length-1) || 
		(textElement.lastIndexOf("--") == textElement.length-1) || 
		(textElement.lastIndexOf("-") == textElement.length-1) || 
		(textElement.lastIndexOf(":") == textElement.length-1) || 
		(textElement.lastIndexOf("&#8212") == textElement.length-1)
	   ) 
  { 
    return "longer";
	} else if (
		(textElement.lastIndexOf(".") == textElement.length-1) || 
		(textElement.lastIndexOf("?") == textElement.length-1) || 
		(textElement.lastIndexOf("!") == textElement.length-1)
		) 
	{
    return "longest";
  } else {
    return "0";
  }
}


function jsreaderSetPace(adjust) {
	if (adjust =="+") {
		jsreaderDelay = jsreaderDelay+50;
	} else {
		jsreaderDelay = jsreaderDelay-50;
	}
	jsreaderPaceNode.value = jsreaderDelay;
}

function jsreaderSetLength(adjust) {
  if (adjust == "+") {
    jsreaderMaxLength = jsreaderMaxLength+1;
  } else {
	if (jsreaderMaxLength>0) {
		jsreaderMaxLength = jsreaderMaxLength-1;
	}
  }
  jsreaderMaxLengthNode.value = jsreaderMaxLength;
	if (jsreaderMaxLength>=18) {
		jsreaderTextField.size = jsreaderMaxLength;
	}
}
function jsreaderPause() {
	document.getElementById("jsreader-context-link-menu").setAttribute("collapsed", false);
	jsreaderStopped = true;
	jsreaderPaused = true;
	jsreaderPauseNode.disabled = true;
	jsreaderPlayNode.disabled = false;
	jsreaderRewindNode.disabled = false;
	jsreaderStopNode.disabled = false;
	
	jsreaderPauseNode.image = pauseDisImg;
	jsreaderPlayNode.image = playImg;
	jsreaderRewindNode.image = rewindImg;
	jsreaderStopNode.image = stopImg;
}
function jsreaderStop() {
	document.getElementById("jsreader-context-link-menu").setAttribute("collapsed", false);
	if (jsreaderPaused == true) {
		jsreaderPaused = false;
	}
	jsreaderN = 0;
	jsreaderStopped = true;
	jsreaderTextField.value = "";
	jsreaderPauseNode.disabled = true;
	jsreaderPlayNode.disabled = false;
	jsreaderRewindNode.disabled = true;
	jsreaderStopNode.disabled = true;
	jsreaderMaxLengthNode.disabled = false;
	jsreaderLess.disabled = false;
	jsreaderMore.disabled = false;
	
	jsreaderPauseNode.image = pauseDisImg;
	jsreaderPlayNode.image = playImg
	jsreaderRewindNode.image = rewindDisImg;
	jsreaderStopNode.image = stopDisImg;
}
function jsreaderRewind() {
	if (jsreaderRewindStopped != true) {
		document.getElementById("jsreader-context-link-menu").setAttribute("collapsed", false);
		if (jsreaderN!=0)	jsreaderN--;
		if (jsreaderN >= 0) {
			if (jsreaderWordArray[jsreaderN]!="&#182;") {
				jsreaderTextField.value = jsreaderWordArray[jsreaderN];
			}
		}
		if (jsreaderN!=0) setTimeout("jsreaderRewind()", 150);
	}
}
function jsreaderStartRewind() {
	jsreaderPause();
	jsreaderRewindStopped = false;
	setTimeout("jsreaderRewind()", 150);
	jsreaderPauseNode.disabled = true;
	jsreaderPlayNode.disabled = false;
	jsreaderStopNode.disabled = false;
	
	
	jsreaderPauseNode.image = pauseDisImg;
	jsreaderPlayNode.image = playImg
	jsreaderStopNode.image = stopImg;
}
function jsreaderSetMouseOut(flag) {
	jsreaderRewindStopped = true;
}
function jsreaderStopRewind() {
	jsreaderRewindStopped = true;
}
function jsreaderSmaller() {
	if (jsreaderReadSize > .8) {
		jsreaderReadSize = jsreaderReadSize - .2;
	}
	jsreaderTextField.style.fontSize = jsreaderReadSize.toString()+"em";
}
function jsreaderBigger() {
	if (jsreaderReadSize < 3) {
		jsreaderReadSize = jsreaderReadSize + .2;
	}
	jsreaderTextField.style.fontSize = jsreaderReadSize.toString()+"em";;
}
function jsreaderLeft() {
	jsreaderTextField.style.textAlign = "left";
	jsreaderLeftNode.disabled = true;
	jsreaderCenterNode.disabled = false;
	
	jsreaderLeftNode.image = leftDisImg;
	jsreaderCenterNode.image = centerImg;
}
function jsreaderCenter() {
	jsreaderTextField.style.textAlign = "center";
	jsreaderLeftNode.disabled = false;
	jsreaderCenterNode.disabled = true;
	
	jsreaderLeftNode.image = leftImg;
	jsreaderCenterNode.image = centerDisImg;
}
